package com.zy.algorithm.v2.sort;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.zy.activity.BaseFragment;
import com.zy.algorithm.v2.R;
import com.zy.algorithm.v2.bean.HeapSimpleNode;
import com.zy.algorithm.v2.bean.SimpleNode;
import com.zy.view.HeapV2View;
import com.zy.view.LineSetNumberView;

import java.util.List;

public abstract class BaseHeapSortFragmentV2 extends BaseFragment {
    @Override
    protected int getLayout() {
        return R.layout.heap_sort_layout_v2;
    }

    protected HeapV2View heapV2View;
    protected LineSetNumberView lineSetNumberView;

    TextView title;
    TextView des;
    List<? extends SimpleNode> list;
    int curIndex = 0;
    protected int endingIndex = 0;
    protected int endingSize = 0;
    protected int[] ends;

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            play();
            Log.d("BaseHeapSortFragmentV2", "curIndex:" + curIndex);
        }
    };

//    Runnable ending = new Runnable() {
//        @Override
//        public void run() {
//            getEndLineSetView().ending(ends, di(), emptyDi(), endingIndex);
//            endingIndex = endingIndex + 2;
//            if (endingIndex < endingSize) {
//                mHandler.postDelayed(ending, 16);
//            } else {
//                getEndLineSetView().ending(ends, di(), emptyDi(), endingSize);
//            }
//        }
//    };


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        heapV2View = view.findViewById(R.id.base_sort_layout_v2_heap_view);
        heapV2View.setLineLength(150);
        lineSetNumberView = view.findViewById(R.id.base_sort_layout_v2_line_set_number);
        lineSetNumberView.setFactor(3);

        title = view.findViewById(R.id.base_sort_layout_v2_title);
        des = view.findViewById(R.id.base_sort_layout_v2_des);
        des.setText("");
        list = getDataList();

        ends = getEnds(list.get(list.size() - 1));
        endingSize = ends.length;

        mHandler.postDelayed(runnable, 1000);


        title.setText(getTitle());
        init(list.get(0));
    }

    private void init(SimpleNode simpleNode) {
        if (simpleNode instanceof HeapSimpleNode) {
            heapV2View.setData(simpleNode.getStepEnd(), simpleNode.getOpIndex(), ((HeapSimpleNode) simpleNode).getSortedIndexStart());
        }
    }

    protected int[] getEnds(SimpleNode simpleNode) {
        return simpleNode.getStepEnd();
    }

    protected HeapV2View getEndLineSetView() {
        return heapV2View;
    }


    protected void play() {
        HeapSimpleNode simpleNode = (HeapSimpleNode) list.get(curIndex);

        if (simpleNode.isChange()) {
            showData(true);
            //move
            heapV2View.moveChange(simpleNode.getChangeFIndex(), simpleNode.getChangeSIndex(), new HeapV2View.AniListener() {
                @Override
                public void aniEnd() {
                    showData(false);
                }
            });

        } else {
            showData(false);
        }
    }

    private void showData(boolean start) {
        if (start) {
            HeapSimpleNode simpleNode = (HeapSimpleNode) list.get(curIndex);
            des.setText(simpleNode.getDes());
            heapV2View.setData(simpleNode.getStepStart(), simpleNode.getOpIndex(), simpleNode.getSortedIndexStart());

            lineSetNumberView.setData(simpleNode.getStepStart(), simpleNode.getOpIndex(), simpleNode.getSortedIndexStart(), di(), emptyDi());

        } else {
            HeapSimpleNode simpleNode = (HeapSimpleNode) list.get(curIndex);
            des.setText(simpleNode.getDes());
            heapV2View.setData(simpleNode.getStepEnd(), simpleNode.getOpIndex(), simpleNode.getSortedIndexEnd());

            lineSetNumberView.setData(simpleNode.getStepEnd(), simpleNode.getOpIndex(), simpleNode.getSortedIndexEnd() , di(), emptyDi());

            curIndex++;
            if (curIndex < list.size()) {
                mHandler.postDelayed(runnable, 50);
            } else {
                //mHandler.postDelayed(ending, 16);
            }
        }
    }

    protected abstract String getTitle();

    abstract List<? extends SimpleNode> getDataList();


    abstract int di();

    abstract int emptyDi();
}
